﻿using System;
using System.Web;
using Ue.Framework.Web;
using Ue.Qcdz.Domain.Membership;
using Ue.Qcdz.Domain.SaleCategorys;
using Ue.Qcdz.Domain.SaleInfos;
using Ue.Qcdz.Domain.ShopArticle;
using Ue.Qcdz.Domain.Shops;
using Ue.Qcdz.Domain.ShopSinglePages;

namespace Ue.Qcdz.Domain
{
    public partial class SiteContext
    {
        #region Static Methods

        public static SiteContext GetSiteContext()
        {
            return SiteContext.GetSiteContext(HttpContext.Current);
        }
        public static SiteContext GetSiteContext(HttpContext context)
        {
            string currentItemKey = "siteContentKey";
            SiteContext siteContext = context.Items[currentItemKey] as SiteContext;
            if (siteContext == null)
            {
                siteContext = new SiteContext();
                HttpContext.Current.Items.Add(currentItemKey, siteContext);
            }

            return siteContext;

        }
        public static void AddRoles(string roleName, string userName)
        {
            if (!System.Web.Security.Roles.RoleExists(roleName))
            {
                System.Web.Security.Roles.CreateRole(roleName);
            }
            System.Web.Security.Roles.AddUserToRole(userName, roleName);
        }
        public static bool UserInRole(string roleName, string userName)
        {
            return System.Web.Security.Roles.IsUserInRole(userName, roleName);
        }

        #endregion

        #region 构造、字段

        private HttpContext httpContext;

        private SiteContext()
            : this(HttpContext.Current) { }

        private SiteContext(HttpContext httpContext)
        {
            this.httpContext = httpContext;
        }

        #endregion

        #region Public Properties

        public HttpContext HttpContext
        {
            get
            {
                if (this.httpContext == null)
                    return new HttpContext(null);

                return this.httpContext;
            }
        }

        private Ue.Qcdz.Domain.Membership.User user;
        public Ue.Qcdz.Domain.Membership.User User
        {

            get
            {
                if (this.user == null)
                {
                    if (this.HttpContext.User.Identity.IsAuthenticated)
                    {
                        IUserRepository repository = Ue.Infrastructure.RepositoryFramework.RepositoryFactory.GetRepository<IUserRepository, User>();
                        user = repository.FindBy(httpContext.User.Identity.Name);
                    }

                }

                return this.user;
            }
        }
        public Shop Shop
        {
            get
            {
                //店铺ID
                if (!string.IsNullOrEmpty(this.ShopId))
                {
                    return ShopService.GetShop(new Guid(this.ShopId));
                }
                //产品类别得到店铺
                if (!string.IsNullOrEmpty(this.CategoryId))
                {
                    SaleCategory cate = SaleCategoryService.GetSaleCategory(new Guid(this.CategoryId));
                    if (cate != null)
                        return cate.Shop;
                }
                //单页文章得到 店铺
                if (!string.IsNullOrEmpty(this.SinglePageId))
                {
                    ShopSinglePage sPage = ShopSinglePageService.GetShopSinglePage(new Guid(this.SinglePageId));
                    if (sPage != null)
                        return sPage.Shop;
                }
                //新闻ID
                if (!string.IsNullOrEmpty(this.NewsId))
                {
                    ShopNews news = ShopNewsService.GetShopNews(new Guid(this.NewsId));
                    if (news != null)
                        return news.Shop;
                }
                //按产品ID
                if (!string.IsNullOrEmpty(this.ProductID))
                {
                    //从产品中 获取店铺
                    SaleInfo product = SaleInfoService.GetSaleInfo(new Guid(this.ProductID));
                    if (product != null)
                        return product.shop;
                }
                //按留言栏目：
                //-------------------------
                //按用户
                if (this.User != null)
                {
                    return ShopService.GetShop(this.user);
                }
                return null;
            }
        }
        #endregion

        #region Public QueryString Properties

        public object EntityKey
        {
            get { return this.httpContext.Request.GetQueryValue<Guid?>("Id"); }
        }
        public string SearchKey
        {
            get { return this.httpContext.Request.GetQueryValue<string>("SearchKey"); }
        }
        public string WebSiteName
        {
            get { return this.httpContext.Request.GetQueryValue<string>("WebSiteName"); }
        }
        public string PageName
        {
            get { return this.httpContext.Request.GetQueryValue<string>("PageName"); }
        }
        public string DivId
        {
            get { return this.httpContext.Request.GetQueryValue<string>("DivId"); }
        }
        public string State
        {
            get { return this.httpContext.Request.GetQueryValue<string>("State"); }
        }
        public string Name
        {
            get { return this.httpContext.Request.GetQueryValue<string>("Name"); }
        }
        public string FeatureName
        {
            get { return this.httpContext.Request.GetQueryValue<string>("FeatureName"); }
        }
        public string Description
        {
            get { return this.httpContext.Request.GetQueryValue<string>("Description"); }
        }
        public string SalteTitle
        {
            get { return this.httpContext.Request.GetQueryValue<string>("SalteTitle"); }
        }
        public string KeyWord
        {
            get { return this.httpContext.Request.GetQueryValue<string>("KeyWord"); }
        }
        public string SalesType
        {
            get { return this.httpContext.Request.GetQueryValue<string>("SalesType"); }
        }
        public string IsTop
        {
            get { return this.httpContext.Request.GetQueryValue<string>("IsTop"); }
        }
        public string IsHot
        {
            get { return this.httpContext.Request.GetQueryValue<string>("IsHot"); }
        }
        public string Status
        {
            get { return this.httpContext.Request.GetQueryValue<string>("Status"); }
        }
        /// <summary>
        /// 商铺ID
        /// </summary>
        public string ShopId
        {
            get
            {
                return this.httpContext.Request.GetQueryValue<string>("ShopId");
            }
        }
        /// <summary>
        /// 产品类别ID
        /// </summary>
        public string CategoryId
        {
            get { return this.httpContext.Request.GetQueryValue<string>("CtId"); }
        }
        /// <summary>
        /// 店铺 单页 ID
        /// </summary>
        public string SinglePageId
        {
            get { return this.httpContext.Request.GetQueryValue<string>("SgId"); }
        }
        /// <summary>
        ///产品ID
        /// </summary>
        public string ProductID
        {
            get { return this.httpContext.Request.GetQueryValue<string>("ProductID"); }
        }
        /// <summary>
        /// 新闻ID
        /// </summary>
        public string NewsId
        {
            get { return this.httpContext.Request.GetQueryValue<string>("NewsId"); }
        }
        public string MediaName
        {
            get { return this.httpContext.Request.GetQueryValue<string>("MediaName"); }
        }

        #endregion

    }
}
